//
//  ViewController.swift
//  SimpleLabel
//
//  Created by yao_yu on 14-8-15.
//  Copyright (c) 2014年 yao_yu. All rights reserved.
//  布局功能
//

import UIKit

extension String{
    func heightForString(width:CGFloat) -> Float{
        let textView = UITextView()
        textView.font = UIFont.systemFontOfSize(14)
        textView.text = self
        let sizeToFit = textView.sizeThatFits(CGSizeMake(width, 999999))
        return Float(sizeToFit.height)
    }
    
    func height2(width:CGFloat, font:UIFont) -> Float{
        let text = self as NSString
        let bounds = text.boundingRectWithSize(CGSizeMake(width, 999999), options: NSStringDrawingOptions.UsesLineFragmentOrigin, attributes: [NSFontAttributeName:font], context: nil)
        return Float(bounds.height)
    }
}


class ViewController: UIViewController {
    
    var myTitle:UILabel?
    var labelDemo:UILabel?
    var textInputText:UITextField?
    var myBorder:UITextField?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        self.view.backgroundColor = UIColor.clearColor()
        
        // 创建布局
        // 输入
        textInputText = UITextField(frame: CGRectMake(0, 0, 300, 20))
        textInputText?.borderStyle = UITextBorderStyle.RoundedRect
        textInputText?.text = "这是一个测试!来自我的大家的问候这是一个测试!来自我的大家的问候这是一个测试!来自我的大家的问候这是一个测试!来自我的大家的问候这是一个测试!来自我的大家的问候这是一个测试!来自我的大家的问候这是一个测试!来自我的大家的问候这是一个测试!来自我的大家的问候这是一个测试!来自我的大家的问候这是一个测试!来自我的大家的问候这是一个测试!来自我的大家的问候这是一个测试!来自我的大家的问候这是一个测试!来自我的大家的问候"
        textInputText?.setTranslatesAutoresizingMaskIntoConstraints(false)
        
        myBorder = UITextField()
        myBorder?.borderStyle = UITextBorderStyle.Line
        self.view.addSubview(myBorder!)

        //标题
        let myFont = UIFont.systemFontOfSize(24)
        myTitle = UILabel()
        myTitle?.font = myFont
        myTitle?.text = "sfdsf"
        myTitle?.textAlignment = NSTextAlignment.Center
        myTitle?.setTranslatesAutoresizingMaskIntoConstraints(false)
        self.view.addSubview(myTitle!)

        
        
        // 多行文本
        labelDemo = UILabel(frame: CGRectMake(0, 30, 300, 20))
        labelDemo?.text = "只"
        labelDemo?.alpha = 0.5
        labelDemo?.font = UIFont.systemFontOfSize(14)
        labelDemo?.lineBreakMode = NSLineBreakMode.ByWordWrapping
        labelDemo?.numberOfLines = 0
        labelDemo?.setTranslatesAutoresizingMaskIntoConstraints(false)
        
        // 按钮
        let button = UIButton.buttonWithType(UIButtonType.System) as UIButton
        //button.backgroundColor = UIColor.blackColor()
        //button.alpha = 0.8
        button.setTitle("更新", forState: UIControlState.Normal)
        button.setTranslatesAutoresizingMaskIntoConstraints(false)
        button.addTarget(self, action: "onUpdate", forControlEvents: UIControlEvents.TouchUpInside)
        
        // 顶部面板
        let topPane = UIView(frame: CGRectMake(0, 0, 300, 20))
        topPane.addSubview(textInputText!)
        topPane.addSubview(button)
        topPane.setTranslatesAutoresizingMaskIntoConstraints(false)
        
        self.view.addSubview(topPane)
        self.view.addSubview(labelDemo!)

        var _views = [String:AnyObject]()
        _views["Input"] = textInputText
        _views["Command"] = button
        
        topPane.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[Input(>=200)]-[Command(60)]|", options: NSLayoutFormatOptions(0), metrics: nil, views: _views))
        topPane.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[Input(>=20)]|", options: NSLayoutFormatOptions(0), metrics: nil, views: _views))
        topPane.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[Command(>=20)]|", options: NSLayoutFormatOptions(0), metrics: nil, views: _views))
        
        _views = ["topPane":topPane, "labelDemo":labelDemo!, "myTitle":myTitle!]
        self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-20-[myTitle(60)]-[topPane(30)]-[labelDemo(>=40)]|", options: NSLayoutFormatOptions(0), metrics: nil, views: _views))
        self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-5-[myTitle(>=200)]-5-|", options: NSLayoutFormatOptions(0), metrics: nil, views: _views))
        self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-5-[topPane(>=200)]-5-|", options: NSLayoutFormatOptions(0), metrics: nil, views: _views))
        self.view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-5-[labelDemo(>=200)]-5-|", options: NSLayoutFormatOptions(0), metrics: nil, views: _views))
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func onUpdate(){
        labelDemo?.text = textInputText?.text
        let width = labelDemo?.bounds.width
        let height = labelDemo?.text.height2(width!, font: UIFont.systemFontOfSize(14))
        let h2 = labelDemo?.text.heightForString(width!)
        let h3 = labelDemo?.bounds.height
        myTitle?.text = NSString(format: "%5.2f, %5.2f, %5.2f", height!, h2!, Float(h3!)) as String
        let bounds = labelDemo?.bounds
        myBorder?.bounds = CGRectMake((bounds?.origin.x)!, 100, (bounds?.width)!, CGFloat( height!))
        
        myBorder?.backgroundColor = UIColor.yellowColor()
    }


}

